﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public enum CardStyle//四种状态
{
    CanPlace,//可种植
    NoCD,
    NoSun,
    NoAll
}
public class UICard : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler,IPointerClickHandler//鼠标进入 移出 点击
{
    public int needSun;
    private Image mask;
    private Image self; //没阳光变灰
    public float cdTime;
    private bool canPlace;//是否可以放植物
    private bool needPlace;//是否需要放植物

    private Base_ plant;//创建的植物
    private Base_ plantIngrid;//透明的植物
    public PlantType planttype;
    private float time;
    public bool CanPlace
    {
        get => canPlace;
        set
        {
            canPlace = value;
            CheckState();
        }
    }
    private void CheckState()//卡牌状态监测
    {
        if (canPlace && PlayerManager.instance.SunCount >= needSun)
            CardStyle = CardStyle.CanPlace;
        if (!canPlace && PlayerManager.instance.SunCount >= needSun)
            CardStyle = CardStyle.NoCD;
        if (canPlace && PlayerManager.instance.SunCount < needSun)
            CardStyle = CardStyle.NoSun;
        if (!canPlace && PlayerManager.instance.SunCount < needSun)
            CardStyle = CardStyle.NoAll;
    }
    public bool NeedPlace
    {
        get => needPlace;
        set
        {
            needPlace = value;
            if (needPlace)
            {
                GameObject go = PlantManager.instance.GetPlantType(planttype);
                plant=Instantiate(go, Vector3.zero, Quaternion.identity, GameObject.Find("Grid").transform).GetComponent<Base_>();
                plant.InitForCreat(false);
            }
            else
            {
                if (plant != null)
                {
                    Destroy(plant.gameObject);
                    plant = null;
                }
            }
        }
    }
    private CardStyle cardStyle=CardStyle.NoAll;

    public CardStyle CardStyle {
        get => cardStyle;
        set {
            if (cardStyle == value) return;
            switch(value)
            {
                case CardStyle.CanPlace:
                    mask.fillAmount = 0;
                    self.color = Color.white;
                    break;
                case CardStyle.NoAll:
                    mask.fillAmount = 1;
                    if (cardStyle == CardStyle.NoCD) return;
                    CdEnter();
                    self.color = new Color(0.75f,0.75f,0.75f);
                    break;
                case CardStyle.NoCD:
                    mask.fillAmount = 1;
                    if (cardStyle == CardStyle.NoAll) return; 
                    CdEnter();
                    self.color = Color.white;
                    break;
                case CardStyle.NoSun:
                    mask.fillAmount = 0;
                    self.color = new Color(0.75f, 0.75f, 0.75f);
                    break;
            }
            cardStyle = value;

        }
    }

    private void Start()
    {
        mask = transform.Find("遮罩").GetComponent<Image>();
        self=transform.GetComponent<Image>();
        CanPlace = true;
        NeedPlace = false;
        plantIngrid = null;
        PlayerManager.instance.AddSun(CheckState);
    }
    /// <summary>
    /// 鼠标移入
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerEnter(PointerEventData eventData)
    {
        if (!CanPlace) return; 
        transform.localScale = new Vector3(1.05f, 1.05f);
    }
    /// <summary>
    /// 鼠标移出
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerExit(PointerEventData eventData)
    {
        if (!CanPlace) return; 
        transform.localScale = new Vector3(1,1); 
    }
    /// <summary>
    /// 进入cd
    /// </summary>
    private void CdEnter()
    {
        mask.fillAmount = 1;//不可以操作
        StartCoroutine(Cd());
        //计算冷却

    }
    private IEnumerator Cd()
    {
        float calCd = (1 / cdTime) * 0.1f;
        time = cdTime;
        while (time>=0)
        {
            yield return new WaitForSeconds(0.1f);
            mask.fillAmount -= calCd;
            time -= 0.1f;
        }
        mask.fillAmount = 0;
        CanPlace = true;
    }

    /// <summary>
    /// 放置植物
    /// </summary>
 
    public void OnPointerClick(PointerEventData eventData)
    {
        if (!CanPlace) return;
        if (!NeedPlace)
            NeedPlace = true;
    }
    private void Update()
    {
        Creat();
    }
    public void Creat()
    {
        if (plant != null && NeedPlace && cardStyle==CardStyle.CanPlace)
        {
            ///植物跟随鼠标
            Vector3 mousePoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Grid grid = GridManager.instance.GetGridByWordPos(mousePoint);//获取网格位置
            plant.transform.position = new Vector3(mousePoint.x, mousePoint.y, 0);
            //距离网格近就产生透明的植物
            if (grid.hasPlant==false&&Vector2.Distance(mousePoint,grid.wordPoint) < 1)
            {
                if (plantIngrid == null)
                {
                    plantIngrid = Instantiate(plant, grid.wordPoint, Quaternion.identity, GameObject.Find("Grid").transform);
                    plantIngrid.InitForCreat(true); 
                } 
                else
                    plantIngrid.transform.position = grid.wordPoint;
                //放置植物
                if (Input.GetMouseButtonDown(0))
                {
                    PlayerManager.instance.SunCount -= needSun;
                    plant.InitForPlace(grid);
                    plant.GetComponent<BoxCollider2D>().enabled = true;
                    plant = null;
                    Destroy(plantIngrid.gameObject);
                    plantIngrid = null;
                    CanPlace = false;
                    NeedPlace = false;
                }
            }
            else
            {
                if(plantIngrid!=null)
                {
                    Destroy(plantIngrid.gameObject);
                    plantIngrid = null;
                }
            }
           
        }
        //取消种植
        if (Input.GetMouseButton(1))
        {
            if (plant != null)
            {
                Destroy(plant.gameObject);
                plant = null;
            }
            if (plantIngrid != null)
            {
                Destroy(plantIngrid.gameObject);
                plantIngrid = null;
            }
            NeedPlace = false;
        }
    }
}
